﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using KPIS.GERP.GBMS.MODEL;
using System.Collections;
using System.Threading;
using System.Security;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Approval_Edit_Expense : System.Web.UI.Page
    {
        private IProjectControl projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private FlexibleFieldBLL FlexibleBLL = new FlexibleFieldBLL();
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        int year = 2554;

        string type;

        public int BgmYear
        {
            set { year = value; }
            get { return year; }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            type = Request.QueryString["type"].ToString();

            lblHeaderID.Text = Request.QueryString["hid"].ToString();
            hdfHeaderID.Value = Request.QueryString["hid"].ToString();
            lbSourceID.Text = Request.QueryString["sid"].ToString();
            hdType.Value = type;

            if (type.Length > 0)
                projectBLL = Project_Factory.GetProjectControlFactory(type);

            if (!IsPostBack)
            {
                if (!Object.Equals(Session["SrtategyID"], null))
                {
                    lblStrategyID.Text = Session["SrtategyID"].ToString();
                }

                if (!Object.Equals(Session["StrategyName"], null))
                {
                    litTitle.Text = Session["StrategyName"].ToString();
                }

                if (type == "md")
                {
                    gridExpense.Columns[3].Visible = false;
                }
                else if (type == "rc")
                {
                    panel_project.Visible = false;
                    btnEditProject.Visible = false;
                    panel_expense.Visible = true;
                    gridExpense.Columns[3].Visible = true;
                }

                LoadData();
            }
        }

        private void LoadData()
        {
            ViewState["gridProject"] = projectBLL.GetDataProject(year, lblHeaderID.Text, lblStrategyID.Text, lbSourceID.Text);

            gridProject.DataSource = (DataTable)ViewState["gridProject"];
            gridProject.DataBind();

            ViewState["gridExpense"] = projectBLL.GetDataProjectExpense(lblStrategyID.Text, lbSourceID.Text);

            gridExpense.DataSource = (DataTable)ViewState["gridExpense"];
            gridExpense.DataBind();

        }

        protected void gridProject_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "CancelClick")
            {
                var dataItem = e.Item as GridDataItem;

                Label lb = gridProject.Items[e.Item.ItemIndex].FindControl("lbFlag") as Label;

                if (lb.Text == "1")
                {
                    dataItem["bgm_project_name"].Style["color"] = "Black";
                    dataItem["total_amount"].Style["color"] = "Black";
                    dataItem["approve_amount"].Style["color"] = "Black";

                    dataItem["bgm_project_name"].Text = dataItem["bgm_project_name"].Text.Substring(0, dataItem["bgm_project_name"].Text.IndexOf("*"));

                    lb.Text = "0";
                }
                else
                {
                    dataItem["bgm_project_name"].Style["color"] = "Red";
                    dataItem["total_amount"].Style["color"] = "Red";
                    dataItem["approve_amount"].Style["color"] = "Red";

                    dataItem["bgm_project_name"].Text = dataItem["bgm_project_name"].Text + "    **** ยกเลิกโครงการ ****";
                    
                    lb.Text = "1";
                }
            }
        }

        protected void btnEditProject_Click(object sender, EventArgs e)
        {
            panel_expense.Visible = false;
            panel_project.Visible = true;
        }

        protected void btnEditExpense_Click(object sender, EventArgs e)
        {
            panel_project.Visible = false;
            panel_expense.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridProject.Items.Count; i++)
            {
                Label lbProjSeq = (Label)gridProject.MasterTableView.Items[i].FindControl("lbProjSeq");
                Label lbCtrlSeq = (Label)gridProject.MasterTableView.Items[i].FindControl("lbCtrlSeq");
                Label lbFlag = (Label)gridProject.MasterTableView.Items[i].FindControl("lbFlag");
                RadNumericTextBox txt = (RadNumericTextBox)gridProject.MasterTableView.Items[i].FindControl("txtApproveAmount");

                try
                {
                    if (lbFlag.Text == "1")
                    {
                        projectBLL.UpdateProject(lbProjSeq.Text, lbCtrlSeq.Text, SystemInfo._CancelRecordStatus, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                    }
                    else
                    {
                        projectBLL.UpdateProjectExpense("", lbCtrlSeq.Text, lbSourceID.Text, "", txt.Text, "", null, null, null, null, null, null, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                    }

                    RadAjaxManager1.Alert("บันทึกเรียบร้อยแล้ว");
                }
                catch
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึกข้อมูล");
                }
            }
        }
    }
}